﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace IronWinter
{
    public class Building : Terrain
    {
        protected int m_maxHealth, m_currHealth, m_armor;
        protected bool m_isSelected;
        protected int buildingDesignate;

        public Player ctrlPlayer; // if 0, contested. 

       // public Building()
        //{ }

        public Building(int x, int y, int width, int length, Texture2D tex, Player controllingPlayer) : base(x, y, width, length, tex) 
        {

            ctrlPlayer = controllingPlayer;
            designate = ctrlPlayer.requestBuildingID();
            m_maxHealth = 2000;
            m_currHealth = m_maxHealth;
            size = 2;
        }
        public int maxHealth
        {
            get { return m_maxHealth; }
            set { m_maxHealth = value; }
        }

        public int currentHealth
        {
            get { return m_currHealth; }
            set { m_currHealth = value; }
        }

        public int armor
        {
            get { return m_armor; }
            set { m_armor = value; }
        }
        public int designate
        {
            get { return buildingDesignate; }
            set { buildingDesignate = value; }
        }
        public Vector2 spawnUnitAt()
        {
            return new Vector2(position.X - Map.cellSize, position.Y);
        }

        public void takeDamage(int dmg, int penetration)
        {
            if (dmg < 0) //healing
            {
                m_currHealth -= dmg;
                if (m_currHealth > m_maxHealth)
                    m_currHealth = m_maxHealth;
            }
            else
            {
                if (penetration < m_armor && (dmg - (m_armor - penetration) > 0))
                    m_currHealth -= (dmg - (m_armor - penetration));
                else
                    m_currHealth -= dmg;
            }

        } //take damage

        public void takeDamage(Unit attacker)
        {
            takeDamage(attacker.atkPow, attacker.armorPen);
        }

        public bool alive
        {
            get { return (m_currHealth >= 0); }
        }

        public Player player
        {
            get { return ctrlPlayer; }
            set { ctrlPlayer = value; }
        }

        public bool isSelected
        {
            get { return m_isSelected; }
            set { m_isSelected = value; }
        }
    
        override public void Draw(SpriteBatch sb)
        {
            if (alive)
            {
                if (isSelected)
                {
                    aniSprite.tint = Color.Green;
                }
                else
                {
                    aniSprite.tint = player.color;
                }
                aniSprite.Draw(sb,position.X,position.Y);

                float percentHealth = (float)m_currHealth / (float)m_maxHealth;
                sb.Draw(Game1.genericTexture, new Rectangle(position.X, position.Y - 10, (int)(Map.cellSize * size), 8), Color.MistyRose);
                sb.Draw(Game1.genericTexture, new Rectangle(position.X, position.Y - 10, (int)(percentHealth * Map.cellSize * size), 8), Color.LightGreen);
            }
        }
    }
}
